﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EGEHaina.Library.Session;

namespace EGEHaina.Library.Controllers
{
    [Messaging]
    public class BaseController : Controller
    {
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);
            this.InitializeFlashMessenger();
        }

        public FlashMessenger Messenger
        {
            get { return (FlashMessenger)Session["FlashMessenger"]; }
            private set { }
        }

        protected void InitializeFlashMessenger()
        {
            if (Messenger == null)
                Session["FlashMessenger"] = new FlashMessenger();
        }

        public void FlashMessage(string message)
        {
            FlashMessage(message, FlashMessenger.MessageType.Success);
        }

        public void FlashMessage(string message, FlashMessenger.MessageType type)
        {
            Messenger.Messages[type].Enqueue(message);
        }

        public string getErrors(ModelStateDictionary modelState)
        {
            string messages = string.Join("<br/>", modelState.Values
                                        .SelectMany(x => x.Errors)
                                        .Select(x => x.ErrorMessage));
            return messages;
        }
    }
}